﻿using System;
using System.Collections.Generic;

namespace homework3
{

    struct Point
    {
        public double x;
        public double y;
    }

    /****UI层****/
    //接口层
    interface GeterInput
    {
        public Point GetInput();
    }
    //实现层
    //控制台手动输入
    class UiGeter : GeterInput
    {
        public Point GetInput()
        {
            Point p;
            p.x = Double.Parse(Console.ReadLine());
            p.y = Double.Parse(Console.ReadLine());
            return p;
        }
    }
    //随机生成矩形
    class RecGeter : GeterInput
    {
        //point副本，用于规划下一个点
        List<Point> points;
        public Point GetInput()
        {
            Point p = new Point();
            switch(points.Count)
            {
                case 0:
                    Random ranx = new Random();
                    p.x = ranx.Next(0, 5);
                    Random rany = new Random();
                    p.y = rany.Next(0, 5);
                    points.Add(p);
                    break;
                case 1:
                    Random length = new Random();
                    p.x = points[0].x + length.Next(0, 10);
                    p.y = points[0].y;
                    points.Add(p);
                    break;
                case 2:
                    Random len = new Random();
                    p.y = points[1].y + len.Next(0, 10);
                    p.x = points[1].x;
                    points.Add(p);
                    break;
                case 3:
                    p.x = points[0].x;
                    p.y = points[2].y;
                    points.Add(p);
                    break;
            }
            return p;
        }
    }
    //随机生成正方形
    class SquGeter : GeterInput
    {
        //point副本，用于规划下一个点
        List<Point> points;
        double length;
        public Point GetInput()
        {
            Point p = new Point();
            switch (points.Count)
            {
                case 0:
                    Random ranx = new Random();
                    p.x = ranx.Next(0, 5);
                    Random rany = new Random();
                    p.y = rany.Next(0, 5);
                    points.Add(p);
                    break;
                case 1:
                    Random len = new Random();
                    length = len.Next(0, 10);
                    p.x = points[0].x + length;
                    p.y = points[0].y;
                    points.Add(p);
                    break;
                case 2:
                    p.y = points[1].y + length;
                    p.x = points[1].x;
                    points.Add(p);
                    break;
                case 3:
                    p.x = points[0].x;
                    p.y = points[2].y;
                    points.Add(p);
                    break;
            }
            return p;
        }
    }
    //随机生成三角形
    class TraGeter : GeterInput
    {
        //point,边长副本，用于规划下一个点
        List<Point> points;
        List<double> length;
        public Point GetInput()
        {
            Point p = new Point();
            switch (points.Count)
            {
                case 0:
                    Random ranx = new Random();
                    p.x = ranx.Next(0, 5);
                    Random rany = new Random();
                    p.y = rany.Next(0, 5);
                    points.Add(p);
                    break;
                case 1:
                    Random len = new Random();
                    double l = len.Next(0, 10);
                    length.Add(l);
                    p.x = points[0].x + l;
                    p.y = points[0].y;
                    points.Add(p);
                    break;
                case 2:
                    Random le = new Random();
                    double s = le.Next(0, (int)length[0]);
                    length.Add(s);
                    //30°角
                    p.x = points[0].x + s/2;
                    p.y = points[0].y + (s*Math.Sqrt(3))/2;
                    points.Add(p);
                    break;
            }
            return p;
        }
    }
    /****逻辑层****/
    //接口层
    public interface Shape
    {
        double Culculate();
        bool CheckShape();
    }
    //实现层
    //矩形
    class Rectangle : Shape
    {
        List<Point> points = new List<Point>();

        //创建矩形
        public Rectangle(GeterInput datain)
        {
            for (int i = 1; i <= 4; i++)
            {
                Point point = new Point();
                point = datain.GetInput();
                points.Add(point);
            }
        }

        //判断形状是否合法
        public bool CheckShape()
        {
            for (int i = 0; i < 4; i++)
            {
                double x1 = points[i].x - points[(i + 1) % 4].x;
                double y1 = points[i].y - points[(i + 1) % 4].y;
                double x2 = points[(i + 1) % 4].x - points[(i + 2) % 4].x;
                double y2 = points[(i + 1) % 4].y - points[(i + 2) % 4].y;
                if (x1 * x2 + y1 * y2 != 0 || (x1==0&&y1==0) )
                {
                    return false;
                }
            }
            return true;
        }

        //计算面积
        public double Culculate()
        {
            double x1 = points[0].x - points[1].x;
            double y1 = points[0].y - points[1].y;
            double x2 = points[1].x - points[2].x;
            double y2 = points[1].y - points[2].y;
            x1 = Math.Sqrt(x1 * x1 + y1 * y1);
            x2 = Math.Sqrt(x2 * x2 + y2 * y2);
            return x1 * x2;
        }

    }
    //正方形
    class Square : Shape
    {
        List<Point> points = new List<Point>();

        //创建正方形
        public Square(GeterInput datain)
        {
            for (int i = 1; i <= 4; i++)
            {
                Point point = new Point();
                point = datain.GetInput();
                points.Add(point);
            }
        }

        //判断形状是否合法
        public bool CheckShape()
        {
            double sizelength = -1;
            for (int i = 0; i < 4; i++)
            {
                double x1 = points[i].x - points[(i + 1) % 4].x;
                double y1 = points[i].y - points[(i + 1) % 4].y;
                double x2 = points[(i + 1) % 4].x - points[(i + 2) % 4].x;
                double y2 = points[(i + 1) % 4].y - points[(i + 2) % 4].y;
                if(sizelength == -1)
                    sizelength = Math.Sqrt(x1 * x1 + y1 * y1);
                if (x1 * x2 + y1 * y2 != 0 || (x1 == 0 && y1 == 0) || sizelength==0 || sizelength!= Math.Sqrt(x1 * x1 + y1 * y1) || sizelength != Math.Sqrt(x2 * x2 + y2 * y2))
                {
                    return false;
                }
            }
            return true;
        }

        //计算面积
        public double Culculate()
        {
            double x1 = points[0].x - points[1].x;
            double y1 = points[0].y - points[1].y;
            double x2 = points[1].x - points[2].x;
            double y2 = points[1].y - points[2].y;
            x1 = Math.Sqrt(x1 * x1 + y1 * y1);
            x2 = Math.Sqrt(x2 * x2 + y2 * y2);
            return x1 * x2;
        }

    }
    //三角形
    class Triangle : Shape
    {
        List<Point> points = new List<Point>();

        //创建正方形
        public Triangle(GeterInput datain)
        {
            for (int i = 1; i <= 3; i++)
            {
                Point point = new Point();
                point = datain.GetInput();
                points.Add(point);
            }
        }

        //判断形状是否合法
        public bool CheckShape()
        {
            for (int i = 0; i < 4; i++)
            {
                double x1 = points[i].x - points[(i + 1) % 3].x;
                double y1 = points[i].y - points[(i + 1) % 3].y;
                double x2 = points[(i + 1) % 3].x - points[(i + 2) % 3].x;
                double y2 = points[(i + 1) % 3].y - points[(i + 2) % 3].y;
                double x3 = points[(i + 2) % 3].x - points[(i + 3) % 3].x;
                double y3 = points[(i + 2) % 3].y - points[(i + 3) % 3].y;
                double size1 = Math.Sqrt(x1 * x1 + y1 * y1);
                double size2 = Math.Sqrt(x2 * x2 + y2 * y2);
                double size3 = Math.Sqrt(x3 * x3 + y3 * y3);
                if ( (x1 == 0 && y1 == 0) || size1 + size2 <= size3)
                {
                    return false;
                }
            }
            return true;
        }

        //计算面积
        public double Culculate()
        {
            double x1 = points[0].x - points[1].x;
            double y1 = points[0].y - points[1].y;
            double x2 = points[1].x - points[2].x;
            double y2 = points[1].y - points[2].y;
            double x3 = points[2].x - points[3].x;
            double y3 = points[2].y - points[3].y;
            x1 = Math.Sqrt(x1 * x1 + y1 * y1);
            x2 = Math.Sqrt(x2 * x2 + y2 * y2);
            x3 = Math.Sqrt(x3 * x3 + y3 * y3);
            double p = (x1 + x2 + x3) / 2;
            return Math.Sqrt(p*(p-x1)*(p-x2)*(p-x3));
        }

    }

    public class ShapeCalculate
    {
        public double culculate(Shape shape)
        {
            return shape.Culculate();
        }
    }

    public class ShapeCheck
    {
        public bool check(Shape shape)
        {
            return shape.CheckShape();
        }
    }


    //工厂生成层
    public class Factory
    {
        public Shape GetShape(int kind , double)
        {

        }
        public List<Shape> GetShape(List<int> shapes)
        {
            List<Shape> goods = new List<Shape>();
            foreach (int i in shapes)
            {
                switch (i)
                {
                    case 1:
                        goods.Add(this.GetRectangle());
                        break;
                    case 2:
                        goods.Add(this.GetSquare());
                        break;
                    case 3:
                        goods.Add(this.GetTriangle());
                        break;
                }
            }
            return goods;

        }

        //生成矩形
        public Shape GetRectangle()
        {
            RecGeter geter = new RecGeter();
            Rectangle r = new Rectangle(geter);
            return r;
        }

        //生成正方形
        public Shape GetSquare()
        {
            SquGeter geter = new SquGeter();
            Square r = new Square(geter);
            return r;
        }

        //生成三角形
        public Shape GetTriangle()
        {
            TraGeter geter = new TraGeter();
            Triangle r = new Triangle(geter);
            return r;
        }

        static void Main(string[] args)
        {
            List<int> a = new List<int>();
            for (int i = 0; i < 10; i++)
            {
                Random ran = new Random();
                a.Add(ran.Next(1, 4));
            }

            Factory f = new Factory();
            List<Shape> shapes = f.GetShape(a);
            double sum = 0;
            shapes.ForEach(a => { sum += a.Culculate(); });
        }
    }



}
